package view;

import javax.swing.*;
import javax.swing.table.*;
import java.sql.*;
import java.util.*;

import model.*;
import control.*;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class NewGenus extends JPanel {
  public static final String NAME = "genus";
  private SnakesCRUD parent;
  
  private ArrayList<Family> families;
  private ArrayList<Genus> genus;
  
  private CGenus genusControl;
  
  public NewGenus(SnakesCRUD parent) {
    this.parent = parent;
    
    families = new ArrayList<Family>();
    genus = new ArrayList<Genus>();
    genusControl = new CGenus(parent.getConnection());
    
    initComponents();
    updateFamilies();
    updateTable();
  }
  
  private final void updateTable() {
    try {
      this.genus = genusControl.retrieveAll();
      
      Object[] o = new Object[6];
      ((DefaultTableModel)(tbGenus.getModel())).setRowCount(0);
      
      for (int i = 0; i < genus.size(); i++) {
        o[0] = genus.get(i).getName();
        o[1] = genus.get(i).getObservations().equals("null")?"":genus.get(i).getObservations();
        o[2] = genus.get(i).getDescription();
        o[3] = genus.get(i).getFamily().getName();
        
        ((DefaultTableModel)(tbGenus.getModel())).addRow(o);
      }
    }
    catch(Exception e) {}
  }
  
  private final void updateFamilies() {
    try {
      this.families = new CFamily(parent.getConnection()).retrieveAll();
      
      Object[] o = new Object[6];
      ((DefaultListModel)(ltFamilies.getModel())).clear();
      
      for (int i = 0; i < families.size(); i++){
    	((DefaultListModel)(ltFamilies.getModel())).addElement(families.get(i).getName());
      }
      
      if (families.size() == 0)
        JOptionPane.showMessageDialog(null, "There is no data on FAMILIES table!", "Snakes", JOptionPane.INFORMATION_MESSAGE);
    }
    catch(Exception e) {}
  }
  
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        {
        	jScrollPane3 = new JScrollPane();
        	{
        		txaObs = new javax.swing.JTextArea();
        		jScrollPane3.setViewportView(txaObs);
        	}
        }
        jLabel4 = new javax.swing.JLabel();
        {
        	jScrollPane4 = new JScrollPane();
        	{
        		txaDesc = new javax.swing.JTextArea();
        		jScrollPane4.setViewportView(txaDesc);
        	}
        }
        jLabel8 = new javax.swing.JLabel();
        GroupLayout layout = new GroupLayout((JComponent)this);
        this.setLayout(layout);
        txtName = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        ltFamilies = new javax.swing.JList();
        btnAdd = new javax.swing.JButton();
        btnRemove = new javax.swing.JButton();
        btnModify = new javax.swing.JButton();
        btnRefresh = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbGenus = new javax.swing.JTable();

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setText("Editing MEMBER Table");

        jLabel2.setText("Name:");

        jLabel3.setText("Observations:");

        jLabel4.setText("Description:");

        jLabel8.setText("Family:");

        ltFamilies.setModel(new DefaultListModel());
        jScrollPane1.setViewportView(ltFamilies);
        ltFamilies.setPreferredSize(new java.awt.Dimension(117, 128));

        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnRemove.setText("Remove");
        btnRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveActionPerformed(evt);
            }
        });

        btnModify.setText("Modify");
        btnModify.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModifyActionPerformed(evt);
            }
        });

        btnRefresh.setText("Refresh");
        layout.setVerticalGroup(layout.createSequentialGroup()
        	.addContainerGap(24, 24)
        	.addComponent(jLabel1, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
        	.addComponent(jScrollPane2, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE)
        	.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
        	.addGroup(layout.createParallelGroup()
        	    .addComponent(jScrollPane1, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 219, GroupLayout.PREFERRED_SIZE)
        	    .addGroup(layout.createSequentialGroup()
        	        .addGroup(layout.createParallelGroup()
        	            .addComponent(txtName, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	            .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                .addComponent(jLabel2, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED))
        	            .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                .addComponent(jLabel8, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)))
        	        .addGap(24)
        	        .addGroup(layout.createParallelGroup()
        	            .addComponent(jScrollPane3, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
        	            .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                .addComponent(jLabel3, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	                .addGap(49)))
        	        .addGap(22)
        	        .addGroup(layout.createParallelGroup()
        	            .addComponent(jScrollPane4, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)
        	            .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                .addComponent(jLabel4, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	                .addGap(54)))))
        	.addGap(25)
        	.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
        	    .addComponent(btnRefresh, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	    .addComponent(btnRemove, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	    .addComponent(btnModify, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	    .addComponent(btnAdd, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
        	.addContainerGap(48, 48));
        layout.setHorizontalGroup(layout.createSequentialGroup()
        	.addContainerGap(29, 29)
        	.addGroup(layout.createParallelGroup()
        	    .addGroup(layout.createSequentialGroup()
        	        .addGroup(layout.createParallelGroup()
        	            .addGroup(layout.createSequentialGroup()
        	                .addGroup(layout.createParallelGroup()
        	                    .addComponent(jLabel3, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	                    .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                        .addComponent(btnAdd, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE)
        	                        .addGap(10))
        	                    .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                        .addComponent(jLabel4, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	                        .addGap(10)))
        	                .addGroup(layout.createParallelGroup()
        	                    .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                        .addComponent(jScrollPane4, GroupLayout.PREFERRED_SIZE, 224, GroupLayout.PREFERRED_SIZE)
        	                        .addComponent(jLabel8, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
        	                    .addGroup(layout.createSequentialGroup()
        	                        .addPreferredGap(jScrollPane4, jScrollPane3, LayoutStyle.ComponentPlacement.INDENT)
        	                        .addGroup(layout.createParallelGroup()
        	                            .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                                .addComponent(jScrollPane3, GroupLayout.PREFERRED_SIZE, 224, GroupLayout.PREFERRED_SIZE)
        	                                .addGap(17))
        	                            .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                                .addGap(19)
        	                                .addComponent(btnRemove, GroupLayout.PREFERRED_SIZE, 82, GroupLayout.PREFERRED_SIZE)
        	                                .addGap(56)
        	                                .addComponent(btnModify, GroupLayout.PREFERRED_SIZE, 84, GroupLayout.PREFERRED_SIZE)))
        	                        .addGap(17))))
        	            .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                .addComponent(jLabel2, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
        	                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
        	                .addComponent(txtName, GroupLayout.PREFERRED_SIZE, 227, GroupLayout.PREFERRED_SIZE)
        	                .addGap(77))
        	            .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                .addComponent(jLabel1, GroupLayout.PREFERRED_SIZE, 160, GroupLayout.PREFERRED_SIZE)
        	                .addGap(199)))
        	        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
        	        .addGroup(layout.createParallelGroup()
        	            .addGroup(layout.createSequentialGroup()
        	                .addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 123, GroupLayout.PREFERRED_SIZE)
        	                .addGap(0, 0, Short.MAX_VALUE))
        	            .addGroup(GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
        	                .addGap(30)
        	                .addComponent(btnRefresh, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE)
        	                .addGap(0, 6, Short.MAX_VALUE))))
        	    .addGroup(layout.createSequentialGroup()
        	        .addComponent(jScrollPane2, GroupLayout.PREFERRED_SIZE, 494, GroupLayout.PREFERRED_SIZE)
        	        .addGap(0, 0, Short.MAX_VALUE)))
        	.addContainerGap(59, 59));
        btnRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshActionPerformed(evt);
            }
        });

        tbGenus.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {},
            new String [] {
                "Name", "Observations", "Description", "Family"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(tbGenus);

    }// </editor-fold>//GEN-END:initComponents

  private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
    if (ltFamilies.getSelectedIndex() != -1) 
    {  
      try {
       
    	Genus genus = new Genus();
    	genus.setName(txtName.getText());
    	genus.setObservations(txaObs.getText());
    	genus.setDescription(txaDesc.getText());
    	genus.setFamily(families.get(ltFamilies.getSelectedIndex()));
    	
    	  
        genusControl.create(genus);
        updateTable();
        JOptionPane.showMessageDialog(null, "Data added!", "Snakes", JOptionPane.INFORMATION_MESSAGE);
      }
      catch(SQLException e) {
        JOptionPane.showMessageDialog(null, e.getMessage(), "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Invalid ID!", "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (ClassNotFoundException e) {}
    }
    else {
      JOptionPane.showMessageDialog(null, "Please, check the information provided", "Snakes", JOptionPane.INFORMATION_MESSAGE);
    }
  }//GEN-LAST:event_btnAddActionPerformed

  private void btnRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveActionPerformed
    if (!txtName.getText().isEmpty()) {
      try {
    	  Genus genus = new Genus();
      	genus.setName(txtName.getText());
    
        genusControl.delete(genus);
        updateTable();
        JOptionPane.showMessageDialog(null, "Data deleted!", "Snakes", JOptionPane.INFORMATION_MESSAGE);
      }
      catch(SQLException e) {
        JOptionPane.showMessageDialog(null, e.getMessage(), "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Invalid ID!", "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (ClassNotFoundException e) {}
    }
    else {
      JOptionPane.showMessageDialog(null, "Please, check the information provided", "Snakes", JOptionPane.INFORMATION_MESSAGE);
    }
  }//GEN-LAST:event_btnRemoveActionPerformed

  private void btnModifyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModifyActionPerformed
        if ( ltFamilies.getSelectedIndex() != -1) 
    {  
      try {
    	  Genus genus = new Genus();
      	genus.setName(txtName.getText());
      	genus.setObservations(txaObs.getText());
      	genus.setDescription(txaDesc.getText());
      	genus.setFamily(families.get(ltFamilies.getSelectedIndex()));
    
        genusControl.update(genus);
        updateTable();
        JOptionPane.showMessageDialog(null, "Data updated!", "Snakes", JOptionPane.INFORMATION_MESSAGE);
      }
      catch(SQLException e) {
        JOptionPane.showMessageDialog(null, e.getMessage(), "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Invalid ID!", "Snakes", JOptionPane.ERROR_MESSAGE);
      }
      catch (ClassNotFoundException e) {}
    }
    else {
      JOptionPane.showMessageDialog(null, "Please, check the information provided", "Snakes", JOptionPane.INFORMATION_MESSAGE);
    }
  }//GEN-LAST:event_btnModifyActionPerformed

  private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed
	updateFamilies();
    updateTable();
  }//GEN-LAST:event_btnRefreshActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnModify;
    private javax.swing.JButton btnRefresh;
    private javax.swing.JButton btnRemove;
    private javax.swing.JTextArea txaDesc;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextArea txaObs;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList ltFamilies;
    private javax.swing.JTable tbGenus;
    private JScrollPane jScrollPane4;
    private JScrollPane jScrollPane3;
    // End of variables declaration//GEN-END:variables
}
